# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Empire;
use strict;

use wxui::widget::Stats;
use wxui::widget::Map;
use wxui::widget::Planet;
use wxui::widget::Fleets;

use layer::Planet;
use layer::Fleet;
use layer::Grid;
use layer::Nation;
use layer::Selection;

use Wx;
use base 'Wx::Panel';
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL wxTOP wxLEFT wxBOTTOM wxALIGN_RIGHT wxALIGN_TOP);

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $id = shift;
	my $self = $class->SUPER::new($parent, $id);
	$self->{Empire} = shift;
	$self->{PlanetView} = 'Geo';
	$self->{FleetView} = 'Example';

	$self->makeLayers();
		
	$self->{Selection} = undef;
	$self->{Displayed} = undef;

	$self->{StatsWidget} = dcExplorer::wxui::widget::Stats->new($self, -1, $self->{Empire});

	$self->{MainPanel} = Wx::Panel->new($self, -1);

	$self->{MapWidget} = dcExplorer::wxui::widget::Map->new($self->{MainPanel}, -1, $self->{Layers}); 
	
	$self->{SidePanel} = Wx::Panel->new($self->{MainPanel}, -1);

	$self->{PlanetPanel} = dcExplorer::wxui::widget::Planet->new($self->{SidePanel}, -1);
	$self->{FleetsList} = dcExplorer::wxui::widget::Fleets->new($self->{SidePanel}, -1);
	$self->displayAt([map {$self->{Empire}->getProp($_)} qw(homeworldx homeworldy)]);
	
	$self->{SidePanel}->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{SidePanel}->{Sizer}->Add($self->{PlanetPanel}, 0,  wxEXPAND, 1);
	$self->{SidePanel}->{Sizer}->Add($self->{FleetsList}, 1, wxEXPAND | wxTOP, 1);
	$self->{SidePanel}->SetSizer($self->{SidePanel}->{Sizer});

	$self->{MainPanel}->{Sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	$self->{MainPanel}->{Sizer}->Add($self->{MapWidget}, 1, wxEXPAND, 1);
	$self->{MainPanel}->{Sizer}->Add($self->{SidePanel}, 0, wxEXPAND | wxLEFT, 1);
	$self->{MainPanel}->SetSizer($self->{MainPanel}->{Sizer});

	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{Sizer}->Add($self->{StatsWidget}, 0, wxEXPAND | wxBOTTOM, 1);
	$self->{Sizer}->Add($self->{MainPanel}, 1, wxEXPAND, 1);
	$self->SetSizer($self->{Sizer});

	return $self;
}

sub makeLayers
{
	my $self = shift;
	my $empire = $self->{Empire};
	my $planetView;
	eval 'use view::planet::'.$self->{PlanetView}.'; $planetView = dcExplorer::view::planet::'.$self->{PlanetView}.'->new()';
	my $fleetView;
	eval 'use view::fleet::'.$self->{FleetView}.'; $fleetView = dcExplorer::view::fleet::'.$self->{FleetView}.'->new()';

	$self->{PlanetLayer} = dcExplorer::layer::Planet->new($planetView, $empire);
	$self->{FleetLayer} = dcExplorer::layer::Fleet->new($fleetView, $empire);
	$self->{SelectionLayer} = dcExplorer::layer::Selection->new($self->{Selection});
	
	my @layers;	
	push @layers, dcExplorer::layer::Grid->new(5, 500);
	push @layers, dcExplorer::layer::Nation->new() if $Settings::ShowNations; 
	push @layers, $self->{PlanetLayer};
	push @layers, $self->{FleetLayer};
	push @layers, $self->{SelectionLayer};

	$self->{Layers} = [@layers];
}

sub getSelection
{
	my $self = shift;
	return $self->{Selection};
}

sub getSelectedFleets
{
	my $self = shift;
	return @{$self->{FleetsList}->getSelectedFleets()};
}

sub getSelectedControlledFleets
{
	my $self = shift;
	return @{$self->{FleetsList}->getSelectedControlledFleets()};
}

sub getSelectedPlanet
{
	my $self = shift;
	return $self->{Empire}->planets->get(@{$self->{Selection}}) if ($self->{Selection});
	return undef;
}

sub getSelectedHarrison
{
	my $self = shift;
	return $self->{Empire}->harrison(@{$self->{Selection}})
		if (defined($self->{Selection}) && $self->{Empire}->planets->get(@{$self->{Selection}})->getProp('my'));
	return undef;
}

sub getSelectedQueue
{
	my $self = shift;
	return $self->{Empire}->queue(@{$self->{Selection}})
		if (defined($self->{Selection}) && $self->{Empire}->planets->get(@{$self->{Selection}})->getProp('my'));
	return undef;
}

sub resetSelection
{
	my $self = shift;
	$self->{Selection} = undef;
	$self->{SelectionLayer}->setSelection(undef);
}

sub switchSelection
{
	my $self = shift;
	my $x = shift;
	my $y = shift;
	my $empire = $self->{Empire};
	if (defined $self->{Selection} && (join ":", @{$self->{Selection}}) eq (join ":", ($x, $y)))
	{
		$self->{Selection} = undef;
	}
	else
	{
		$self->{Selection} = [$x, $y];
	}
	$self->{SelectionLayer}->setSelection($self->{Selection});
	$self->{MapWidget}->Refresh;
	$self->displayAt([$x, $y]);
}

sub displayAt
{
	my $self = shift;
	my $point = shift;
	my $empire = $self->{Empire};
	if ($point)
	{
		return if (defined $self->{Displayed} && (join ":", @{$self->{Displayed}}) eq (join ":", @$point));
	}
	else
	{
		return unless $self->{Displayed};
	}
	$self->{Displayed} = $point;
	$self->refreshFleets;
	$self->refreshPlanet;
}

sub setEmpire
{
	my $self = shift;
	$self->{Empire} = shift;
	$self->{Displayed} = $self->{Selection} || [map {$self->{Empire}->getProp($_)} qw(homeworldx homeworldy)];
	$self->{StatsWidget}->setEmpire($self->{Empire});
	$self->{PlanetLayer}->setEmpire($self->{Empire});
	$self->{FleetLayer}->setEmpire($self->{Empire});
	$self->{MapWidget}->setLayers($self->{Layers});
	$self->refreshFleets;
	$self->refreshPlanet;
}

sub refreshFleets
{
	my $self = shift;
	my $empire = $self->{Empire};
	
	if ($self->{Displayed})
	{
		$self->{FleetsList}->setFleets(
			[$empire->fleets->getFleetsByLocation(@{$self->{Displayed}})],
			[$empire->shared_fleets->getFleetsByLocation(@{$self->{Displayed}})],
			[$empire->alien_fleets->getFleetsByLocation(@{$self->{Displayed}})]
		);
	}
	else
	{
		$self->{FleetsList}->clear();
	}
}

sub refreshPlanet
{
	my $self = shift;
	my $empire = $self->{Empire};
	
	if ($self->{Displayed})
	{
		$self->{PlanetPanel}->setPlanet($empire->planets->get(@{$self->{Displayed}}));
	}
	else
	{
		$self->{PlanetPanel}->setPlanet(undef);
	}
}

sub refreshAll
{
	my $self = shift;
	$self->{StatsWidget}->setEmpire($self->{Empire});
	$self->{PlanetLayer}->setEmpire($self->{Empire});
	$self->{FleetLayer}->setEmpire($self->{Empire});
	$self->{MapWidget}->setLayers($self->{Layers});
	$self->refreshFleets();
	$self->refreshPlanet();
}

sub moveTo
{
	my $self = shift;
	my $x = shift;
	my $y = shift;
	$self->{MapWidget}->MoveTo($x, $y);
}

1;
